/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange;

import com.googlecode.genericdao.search.Search;
import org.crystalrange.model.Transaction;
import org.crystalrange.service.TransactionService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Window;

public abstract class BaseController extends GenericForwardComposer {

    protected TransactionService transactionService;
    protected Transaction executedTransaction;
    protected AnnotateDataBinder binder;
    protected Bandbox bandboxExecutedTransaction;
    protected Label labelTitle;
    protected Label labelMessage;
    protected Image imageMessage;
    protected Message message;
    
    public Transaction getExecutedTransaction() {
        return executedTransaction;
    }

    public void setExecutedTransaction(Transaction executedTransaction) {
        this.executedTransaction = executedTransaction;
    }

    public TransactionService getTransactionService() {
        return this.transactionService;
    }

    public void setTransactionService(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    public Message getMessage() {
        return this.message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }
    
    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.sessionScope.containsKey("CRYSTALRANGE_MESSAGE")) {
            this.message = (Message) this.sessionScope.get("CRYSTALRANGE_MESSAGE");
            this.sessionScope.remove("CRYSTALRANGE_MESSAGE");
        } else {
            this.message = new Message();
        }            
        
        component.setAttribute("controller", this);
    }

    public void onClick$toolbarbuttonNewSession(Event event) throws Exception {
        this.newSession();
    }

    public void onOpen$bandboxExecutedTransaction(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/transactionSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.executedTransaction = (Transaction) this.execution.getAttribute("selectedTransaction");
        }

        this.binder.loadAll();
    }

    public void onOK$bandboxExecutedTransaction(Event event) throws Exception {
        this.transactionExecute();
    }

    public void onChange$bandboxExecutedTransaction(Event event) throws Exception {
        this.executedTransaction = this.transactionService.findByCode(
                this.bandboxExecutedTransaction.getValue());
        this.binder.loadAll();
    }

    public void onClick$toolbarbuttonTransactionExecute(Event event) throws Exception {
        this.transactionExecute();
    }

    private void transactionExecute() throws Exception {
        if (this.executedTransaction != null) {
            this.execution.sendRedirect(this.executedTransaction.getLocation());
        }
    }
    
    private void newSession() throws Exception {
        Clients.evalJavaScript("window.open('" + this.execution.encodeURL("/pages/menu.zul") + "', '_blank');");
    }
}
